<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class FangweiController extends BaseController {
	/*
	 * 防伪验证页面一
	 */
    public function index(){
		$fweinum = generate_fwei();
		$this->assign('fweima',$fweinum);
		/*来自自建码和普通码的伪防伪*/
		if(I('get.self')==1){
			$usertoken = I('get.fromwho');
			$id = I('get.id');
			$this->assign('selffw',1);
			$this->assign('selfid',$id);
			$this->assign('usertoken',$usertoken);
			$this->assign('fromwho',$usertoken);
			/*查询次数+1*/
			$resfwei = D(C('TABLE_SELFFWEI'));
			$r = $resfwei->where(array('user_token'=>$usertoken))->find();
			if($r){
				$rcount = $r['cx'];
				$d['cx'] = $rcount+1;
				$resfwei->where(array('user_token'=>$usertoken))->save($d);
			}

		}else{
			$domainid = I('get.domainid',0);
			/*获取防伪码*/

			/*对domainid和防伪码验证*/
			if($domainid!=0){
				$fwei = D(C('TABLE_FWEI'));
				$res = $fwei->where(array('ma_domainid'=>$domainid,'ma_sn'=>$fweinum))->select();
				if(empty($res)){
					$fwei->ma_domainid = $domainid;
					$fwei->ma_sn = $fweinum;
					$r = $fwei->add();
					if($r){
						/*
                         * 插入查询次数（码表）
                         */
						$type = D(C('TABLE_TYPE'));
						$tres = $type->where(array('domainid'=>$domainid))->find();
						if(!empty($tres['cx'])){
							$cx_num = $tres['cx'];
						}else{
							$cx_num = 0;
						}
						$data['cx'] = $cx_num+1;
						$type->where(array('domainid'=>$domainid))->save($data);
					}
				}
				$this->assign('domainid',$domainid);
				$this->assign('rootid',I('get.rootid'));
			}
		}
		$this->display();
    }
    
    /*
     * 查询页面
     */
    public function chaxun(){
		$selfhelp = I('get.selfhelp');
		if(!empty($selfhelp)){
			/*自建码进来的防伪信息*/
			$fweima = I('get.fweima');
			$user_token = I('get.fromwho');
			$id = I('get.selfid');
			$resfwei = D(C('TABLE_SELFFWEI'));
			$getresult = $resfwei->where(array('user_token'=>$user_token))->find();
			$r = array();
			$r['epname'] = $getresult['epname'];
			$r['ep_sc'] = $getresult['epbuild'];
			$r['ep_addres'] = $getresult['epaddres'];
			$r['ep_code'] = $getresult['ep_code'];
			$r['ep_phone'] = $getresult['eptel'];
			$r['cx'] = $getresult['cx'];
			$this->assign('res',$r);
			$this->assign('selfids',$id);
		}else{
			/*普通码进来的防伪信息*/
			$domainid = I('get.domainid',0);
			if(!empty($domainid)!=0){
				$type = D(C('TABLE_TYPE'));
				$res = $type->where(array('domainid'=>$domainid))->find();
				$this->assign('res',$res);

			}
		}
		$this->display();

    }

}